package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public List<Category> queryByParentId(@RequestParam(name = "pid", defaultValue = "0") Long pid) {
        List<Category> list = this.categoryService.queryByParentId(pid);
        if (list != null) {
            return list;
        }
        return null;
    }

    /**
     * 修改品牌,品牌的所属分类的回显
     *
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> categories = this.categoryService.queryByBrandId(bid);

        if (categories != null && categories.size() != 0) {
            return ResponseEntity.status(HttpStatus.OK).body(categories);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据cid查询对应的分类
     *
     * @param ids
     * @return
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids) {
        List<String> names = this.categoryService.queryNamesByIds(ids);
        if (names != null && names.size() != 0) {
            return ResponseEntity.status(HttpStatus.OK).body(names);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
